home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-04 | 32.3 KB | 1,281 lines | [TEXT/MPS ] |
- /*
- * File: AERequest.cp
- *
- * Contains: xxx put contents here xxx
- *
- * Written by: Rick Violet
- *
- * Copyright: © 1992 by Apple Computer, Inc., all rights reserved.
- *
- * Change History (most recent first):
- *
- * <1+> 5/18/93 RV Now accepts parameters of type 'true', 'false', and 'BOOL' from
- * the Apple Events
- * <7+> 11/19/92 RV
- * 11/18/92 RV xxx put comment here xxx
- *
- * To Do:
- */
-
- #ifndef __APPLICATION__
- #include "Application.h"
- #endif
-
- #ifndef __AERequest__
- #include "AERequest.h"
- #endif
-
- #ifndef __RequestDispatcher__
- #include "RequestDispatcher.h"
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __PACKAGES__
- #include <Packages.h>
- #endif
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // Global Variables
- //—————————————————————————————————————————————————————————————————————————————————————
- extern RequestDispatcher* gTheRequestDispatcher;
- extern Application* gTheApplication;
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequestHandler - handles 'v.u.' 'extc' apple event
- //—————————————————————————————————————————————————————————————————————————————————————
- pascal OSErr
- AERequestHandler( AppleEvent* pMessage, AppleEvent* pReply, long )
- {
- OSErr tErr = noErr;
- AERequest* tReq;
- AEKeyword tVUEventClass = kVUAETool;
- AEDesc tDesc;
- Boolean tUseStandardReplyType;
-
-
- //———— APPLESCRIPT COMPATIBILITY CHECK:
- //———— Use the AppleScript protocol unless we must use the VU 2.0.x protocol.
- //———— A target parameter (noErr) means Virtual User sent the request.
- //———— An error (-1701) means AppleScript or other non-VU entity.
- tUseStandardReplyType = true;
- tErr = AEGetParamDesc( pMessage, kVUAESrvcTarget, typeWildCard, &tDesc );
-
- if( tErr == noErr )
- {
- //———— Free the memory occupied by the target parameter AEDesc.
- AEDisposeDesc( &tDesc );
-
- //———— No protocol version parameter (-1701) means VU 2.0 or 2.0.1.
- tErr = AEGetParamDesc( pMessage, kVUAEProtocolVersion, typeWildCard, &tDesc );
- if( tErr == errAEDescNotFound )
- {
- tUseStandardReplyType = false;
- }
- else
- {
- //———— Free the memory occupied by the version parameter AEDesc.
- AEDisposeDesc( &tDesc );
- }
- }
- else
- {
- //———— reset tErr because this error was informative, not fatal
- tErr = noErr;
- }
-
- if( !tUseStandardReplyType )
- {
- //———— Change reply class id to 'v.u.' for VU 2.0.x compatibility
- //———— Do this as early as possible before errors can occur
- tErr = AEPutAttributePtr( pReply,
- keyEventClassAttr,
- typeType,
- (Ptr) &tVUEventClass,
- sizeof( OSType ) );
-
- if( tErr != noErr )
- {
- //———— error encountered, return err code to AE Manager
- return tErr;
- }
- }
-
- //———— Construct AERequest and put it in the request queue
- tReq = new AERequest( pMessage, pReply, tUseStandardReplyType );
- if( tReq != nil )
- {
- if( tReq->IsACancelRequest() )
- {
- //———— VU does not need a reply from a Cancel request
- //———— so we can delete it before it is suspended.
- gTheRequestDispatcher->DoCancelReq( tReq );
- delete tReq;
- return noErr;
- }
-
- tErr = tReq->Initialize();
- if( tErr != noErr )
- {
- delete tReq;
- }
- else
- {
- gTheRequestDispatcher->QueueRequest( tReq );
- }
- }
- else
- {
- //———— assume a memFullErr if new AERequest failed
- tErr = -108;
- }
-
- return( tErr );
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::AERequest - constructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- AERequest::AERequest( AppleEvent* pMessage, AppleEvent* pReply, Boolean pUseStandardReplyType )
- {
- fMessagePtr = pMessage;
- fReplyPtr = pReply;
- fSuspended = false;
- fReturnID = 0;
- fHaveResetTimerMsg = false;
- fSendResetTicks = TickCount(); //———— Set to current Ticks
- fUseStandardReplyType = pUseStandardReplyType;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::~AERequest - destructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- AERequest::~AERequest()
- {
-
- //———— Only if we successfully suspended the AppleEvent
- //———— and we did NOT successfully resume it are we
- //———— responsible for disposing of the AppleEvent descriptors;
- //———— Otherwise, the AppleEvent Manager automatically disposes of them
- //———— when we return from the Handler (or resume after suspending).
- if( fSuspended )
- {
- AEDisposeDesc( fMessagePtr );
- AEDisposeDesc( fReplyPtr );
- }
-
- //———— if we created a TimeOut Reset AppleEvent, then dispose of it
- if( fHaveResetTimerMsg )
- {
- AEDisposeDesc( &fResetTimerMsg );
- }
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::GetIdentifierOfRequesttoCancel - return the request
- // identifier of the request to cancel.
- // This method is for use with cancel requests only.
- //—————————————————————————————————————————————————————————————————————————————————————
- long
- AERequest::GetIdentifierOfRequesttoCancel()
- {
- ScriptValue* tVal;
- ValueKind tVKind = kVUAnyKind;
- long tResult = 0;
-
- GetNthParam( 1, tVal, tVKind );
- if( tVal )
- {
- switch( tVKind )
- {
- case kVULongNumberKind:
- {
- tResult = ((VULongNumber*)tVal)->GetNumber();
- }
- break;
-
- case kVUStringKind:
- {
- stringtonum( ((VUString*)tVal)->GetText(), &tResult );
- }
- break;
- }
- }
-
- return tResult;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::InstallAppleEventHandler - Install an AppleEvent Handler
- // into the AE dispatch table for receiving
- // Apple Events from V.U.
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- AERequest::InstallAppleEventHandler()
- {
- return AEInstallEventHandler( kVUAETool,
- kVUAESendService,
- NewAEEventHandlerProc(AERequestHandler),
- 0, false );
-
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::SetErrorCode - set the Error code for this request
- //—————————————————————————————————————————————————————————————————————————————————————
- void
- AERequest::SetErrorCode( OSErr tErr )
- {
- if( fReplyPtr != nil )
- {
- AEPutAttributePtr( fReplyPtr,
- keyErrorNumber,
- typeShortInteger,
- (Ptr)&tErr,
- sizeof( OSErr ) );
- }
-
- Request::SetErrorCode( tErr );
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::SetErrorMessage - set the Error message for this request
- //—————————————————————————————————————————————————————————————————————————————————————
- void
- AERequest::SetErrorMessage( char* tErrText )
- {
- if( fReplyPtr != nil )
- {
- AEPutAttributePtr( fReplyPtr,
- keyErrorString,
- typeChar,
- (Ptr)tErrText,
- strlen( tErrText ) );
- }
-
- Request::SetErrorMessage( tErrText );
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::Initialize - initialize the Request
- // Transfers info in AppleEvents to the
- // Request object
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- AERequest::Initialize()
- {
- OSErr tErr = noErr;
-
- //———— Suspend processing of the AppleEvent by the AE Manager.
- //———— This prevents the reply AppleEvent from being sent back and deleted
- //———— when this routine returns ( actually it's when the AE Handler returns ).
- tErr = AESuspendTheCurrentEvent( fMessagePtr );
- if( tErr != noErr )
- {
- fSuspended = false;
- return tErr;
- }
- else
- {
- fSuspended = true;
- }
-
- //———— see AERequest.cp and Request.cp for details
- FixAEManagerBugPart1();
-
-
- //———— Get the return ID of the AppleEvent
- tErr = SetupReturnID();
- if( tErr != noErr )
- {
- SetErrorCode( tErr );
- SetErrorMessage( "Failed to extract return id from AppleEvent." );
- return tErr;
- }
-
- //———— Get the Service Indentifier from the AppleEvent
- //———— This string identifies which service is requested
- tErr = SetupSeviceIdentifier();
- if( tErr != noErr )
- {
- SetErrorCode( tErr );
- SetErrorMessage( "Failed to extract service identifier." );
- return tErr;
- }
-
- //———— Get the service parameters from the AppleEvent
- //———— and install them into this Request.
- tErr = SetupParameterList();
- if( tErr != noErr )
- {
- SetErrorCode( tErr );
- SetErrorMessage( "Failed to extract parameters." );
- return tErr;
- }
-
- //———— Get the service parameters from the AppleEvent
- //———— and install them into this Request.
- tErr = SetupRequestIdentifier();
- if( tErr != noErr )
- {
- SetErrorCode( tErr );
- SetErrorMessage( "Failed to extract return ID." );
- return tErr;
- }
-
- //———— Call Parent class's Initialize
- tErr = Request::Initialize();
- if( tErr != noErr )
- {
- return tErr;
- }
-
- return noErr;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::SetupReturnID - Get the AppleEvent's return ID
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- AERequest::SetupReturnID()
- {
- OSErr tErr;
- long tResult;
- DescType tActualType;
- Size tActualSize;
-
- tErr = AEGetAttributePtr( fMessagePtr,
- keyReturnIDAttr,
- typeLongInteger,
- &tActualType,
- (Ptr)&tResult,
- sizeof( tResult ),
- &tActualSize );
- if( tErr == noErr )
- {
- fReturnID = tResult;
- }
- else
- {
- fReturnID = 0;
- }
- return tErr;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::SetupSeviceIdentifier - Get the AppleEvent's direct parameter
- // It contains the text which identifies
- // which service is requested
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- AERequest::SetupSeviceIdentifier()
- {
- OSErr tErr;
- char tText[256];
- DescType tActualType;
- Size tActualSize;
-
- tErr = AEGetParamPtr( fMessagePtr,
- kVUAESrvcName,
- typeChar,
- &tActualType,
- (Ptr)tText,
- 255,
- &tActualSize );
- if( tErr == noErr )
- {
- tText[tActualSize] = '\0'; //———— Terminate C string with null character
- SetWhichService( tText );
- }
- else
- {
- SetWhichService( nil );
- }
- return tErr;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::SetupParameterList - Get the AppleEvent's 'extp' parameter
- // It contains the list of parameters for the Service
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- AERequest::SetupParameterList()
- {
- OSErr tErr;
- AEDescList tParamDesc;
- VUList* tParamValue;
-
- tErr = AEGetParamDesc( fMessagePtr,
- kVUAESrvcParameters,
- typeAEList,
- &tParamDesc );
- if( tErr != noErr )
- {
- //———— Some services may have no parameters,
- //———— so allow errAEDescNotFound error
- if( tErr == errAEDescNotFound )
- {
- return noErr;
- }
- else
- {
- return tErr;
- }
- }
- else
- {
- tErr = MakeVUListFromDescList( &tParamDesc, &tParamValue );
- if( tParamValue != nil )
- {
- SetParameterList( tParamValue );
- }
- AEDisposeDesc( &tParamDesc );
- }
- return tErr;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::SetupRequestIdentifier - Get the AppleEvent's return ID
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- AERequest::SetupRequestIdentifier()
- {
- OSErr tErr;
- long tReturnID;
- DescType tActualType;
- Size tActualSize;
-
- tErr = AEGetAttributePtr( fMessagePtr,
- keyReturnIDAttr,
- typeLongInteger,
- &tActualType,
- (Ptr)&tReturnID,
- sizeof( long ),
- &tActualSize );
- if( tErr == noErr )
- {
- //———— Set the Request Identifier
- SetRequestIdentifier( tReturnID );
- }
- else
- {
- //———— Set the Request Identifier to zero
- SetRequestIdentifier( 0 );
- }
- return tErr;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::MakeVUListFromDescList - Convert the AEDescList
- // to a VUList object.
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- AERequest::MakeVUListFromDescList( AEDescList* pDescList, VUList** pValue )
- {
- OSErr tErr;
- long tParamCount;
- long i;
- AEKeyword tActualKey;
- DescType tActualType;
- Size tActualSize;
- short tShort;
- long tLong;
- Boolean tBoolean;
- char* tText;
- AEDescList tDescList;
- VUList* tReturnVUList;
- VUList* tVUList;
-
-
- //———— Construct the VUList object to eventually return
- tReturnVUList = new VUList();
- if( tReturnVUList == nil )
- {
- pValue = nil;
- return memFullErr;
- }
-
- //———— Get the number of Descriptors in the list
- tErr = AECountItems( pDescList, &tParamCount );
- if( tErr != noErr )
- {
- pValue = nil;
- return tErr;
- }
-
- //———— For each Descriptor in the list
- for( i = 1; i <= tParamCount; i++ )
- {
- //———— Determine the type of the ith Descriptor
- tErr = AESizeOfNthItem( pDescList, i, &tActualType, &tActualSize );
- if( tErr != noErr )
- {
- break;
- }
-
- //———— Depending on the type,
- //———— Extract the data and put it into ScriptValue
- switch( tActualType )
- {
- //————————————————————————————————————————————————————
- //———— typeBoolean, typeFalse, typeTrue
- case typeBoolean:
- case typeTrue:
- case typeFalse:
- {
- //———— typeBoolean
- if( tActualType == typeBoolean )
- {
- tErr = AEGetNthPtr( pDescList,
- i,
- tActualType,
- &tActualKey,
- &tActualType,
- (Ptr)&tBoolean,
- sizeof(Boolean),
- &tActualSize );
- if( tErr == noErr )
- {
- tReturnVUList->PutNthItem( tBoolean ); //———— defaults to appending
- }
- }
- else
- {
- //———— typeFalse & typeFalse
- tReturnVUList->PutNthItem( (Boolean)(tActualType == typeTrue) ); //———— defaults to appending
- }
- }
- break;
-
- //————————————————————————————————————————————————————
- //———— typeShortInteger
- case typeShortInteger:
- {
- tErr = AEGetNthPtr( pDescList,
- i,
- typeShortInteger,
- &tActualKey,
- &tActualType,
- (Ptr)&tShort,
- sizeof(short),
- &tActualSize );
- if( tErr == noErr )
- {
- tReturnVUList->PutNthItem( tShort ); //———— defaults to appending
- }
- }
- break;
-
- //————————————————————————————————————————————————————
- //———— typeLongInteger
- case typeLongInteger:
- {
- tErr = AEGetNthPtr( pDescList,
- i,
- typeLongInteger,
- &tActualKey,
- &tActualType,
- (Ptr)&tLong,
- sizeof(long),
- &tActualSize );
- if( tErr == noErr )
- {
- tReturnVUList->PutNthItem( tLong ); //———— defaults to appending
- }
- }
- break;
-
- //————————————————————————————————————————————————————
- //———— typeChar
- case typeChar:
- {
- tText = new char[ tActualSize + 1 ];
- if( tText )
- {
- tErr = AEGetNthPtr( pDescList,
- i,
- typeChar,
- &tActualKey,
- &tActualType,
- (Ptr)tText,
- tActualSize + 1,
- &tActualSize );
- if( tErr == noErr )
- {
- tText[tActualSize] = '\0';
- tReturnVUList->PutNthItem( tText ); //———— defaults to appending
- }
- delete tText;
- }
- }
- break;
-
- //————————————————————————————————————————————————————
- //———— typeAEList
- //———— We have to make a recursive call to this same method
- //———— in order to accomplish translation of an AEDescList
- case typeAEList:
- {
- //———— Extract the AEDescList
- tErr = AEGetNthDesc( pDescList,
- i,
- typeAEList,
- &tActualKey,
- &tDescList );
- if( tErr == noErr )
- {
- //———— Now convert the DescList into a VUList
- tErr = MakeVUListFromDescList( &tDescList, &tVUList );
- if( tErr == noErr )
- {
- if( tVUList != nil )
- {
- tReturnVUList->PutNthItem( tVUList ); //———— defaults to appending
- }
- }
- AEDisposeDesc( &tDescList );
- }
- }
- break;
-
- //————————————————————————————————————————————————————
- //———— default case, unknown data type in AEDesc
- //———— use VUNull to represent an 'undefined'
- //———— It also forms a place holder so list
- //———— elements retail their order, and errors are
- //———— isolated with less grief
- default:
- {
- tReturnVUList->PutNthItem( new VUNull() );
- }
- break;
-
- }
-
- //———— Break out of FOR loop if an error was encountered
- if( tErr != noErr )
- {
- break;
- }
- }
- *pValue = tReturnVUList;
- return tErr;
- }
-
- /*
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::AskForMoreTime - inform V.U. to not let this AppleEvent
- // time out for the indicated number of seconds
- //—————————————————————————————————————————————————————————————————————————————————————
- void
- AERequest::AskForMoreTime( unsigned long pNewTimeOutInterval )
- {
- if( !HasBeenCanceled() )
- {
- if( fSendResetTicks - TickCount() < kResetSendThreshold )
- {
- //———— reset the tool's time-out counter for this request
- ResetTimeOutCounter( pNewTimeOutInterval );
-
- //———— reset V.U.'s time-out counter for this request
- SendTimeOutResetMessage( pNewTimeOutInterval );
- }
- }
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::ResetTimeOutCounter - inform V.U. to not let this Request time out
- //—————————————————————————————————————————————————————————————————————————————————————
- void
- AERequest::ResetTimeOutCounter( unsigned long pNewTimeOutInterval )
- {
- //———— Set up the Time Manager task to fire again at a later time
- if( !HasBeenCanceled() )
- {
- //———— Keep Track of when we'll time out
- fSendResetTicks = TickCount() + (pNewTimeOutInterval * 60);
- }
- }
- */
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::ResetTimeOutCounter - inform V.U. to not let this AppleEvent
- // time out for the indicated number of seconds
- //—————————————————————————————————————————————————————————————————————————————————————
- void
- AERequest::ResetTimeOutCounter( unsigned long pNewTimeOutInterval )
- {
- const unsigned long kResetSendThreshold = 600;
-
- if( !HasBeenCanceled() )
- {
- if( fSendResetTicks - TickCount() < kResetSendThreshold )
- {
- //———— Send message to V.U. to reset the time out counter
- SendTimeOutResetMessage( pNewTimeOutInterval );
-
- //———— Call Parent class method
- Request::ResetTimeOutCounter( pNewTimeOutInterval );
- }
- }
- }
-
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::SendTimeOutResetMessage - Send an AppleEvent to V.U. to
- // reset the Time Out counter for this AppleEvent
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- AERequest::SendTimeOutResetMessage( unsigned long pNewTimeOutInterval )
- {
- OSErr tErr;
- AEAddressDesc tAddressDesc;
- AppleEvent tDummyReply;
-
- //———— If we have not already built an event for resetting the timer, do so now
- if( !fHaveResetTimerMsg )
- {
- //———— Get the Target Address Descriptor from the existing default reply AppleEvent
- tErr = AEGetAttributeDesc( fReplyPtr,
- keyAddressAttr,
- typeWildCard,
- &tAddressDesc );
- if( tErr != noErr )
- {
- //———— not much further we can do, except return the error code
- return tErr;
- }
-
-
- //———— Construct the AppleEvent for sending to V.U.
- tErr = AECreateAppleEvent( kVUAETool,
- kVUAEWaitLonger,
- &tAddressDesc,
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &fResetTimerMsg );
- //———— We no longer need the Address descriptor
- AEDisposeDesc( &tAddressDesc );
- if( tErr != noErr )
- {
- //———— not much further we can do, except return the error code
- return tErr;
- }
-
- //———— Put the same return ID as the original AppleEvent from V.U.
- //———— V.U. uses the return ID to determine which AppleEvent needs its timer reset
- tErr = AEPutAttributePtr( &fResetTimerMsg,
- keyReturnIDAttr,
- typeLongInteger,
- (Ptr)&fReturnID,
- sizeof( long ) );
-
- //———— We now have an AppleEvent that we can send, and we need to dispose of later
- fHaveResetTimerMsg = true;
- }
-
- //———— Put the amount of time required to complete the current service request
- //———— into the AppleEvent
- tErr = AEPutParamPtr( &fResetTimerMsg,
- kVUAEWaitAmount,
- typeMagnitude,
- (Ptr)&pNewTimeOutInterval,
- sizeof( pNewTimeOutInterval ) );
- if( tErr != noErr )
- {
- //———— not much further we can do, except return the error code
- return tErr;
- }
-
-
- tErr = AESend( &fResetTimerMsg,
- &tDummyReply,
- kAENoReply + kAENeverInteract,
- kAEHighPriority,
- kAEDefaultTimeout,
- nil,
- nil );
- if( tErr != noErr )
- {
- //———— not much further we can do, except return the error code
- return tErr;
- }
-
- return noErr;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::SendResult - send the result back to VU
- //—————————————————————————————————————————————————————————————————————————————————————
- void
- AERequest::SendResult()
- {
- OSErr tErr = noErr;
-
- //———— If a return value exists, install it in the reply event
- if( fReturnValue != nil )
- {
- tErr = PutReturnValueIntoReplyEvent();
- }
-
- if( tErr != noErr )
- {
- DebugStr("\pAERequest::SendResult Error in PutReturnValueIntoReplyEvent().");
- }
-
- //———— Resume the processing of this AppleEvent pair
- //———— This causes the reply event to be sent back to V.U.
- //———— The AE Manager will delete the request and reply Apple events.
- tErr = AEResumeTheCurrentEvent( fMessagePtr,
- fReplyPtr,
- NewAEEventHandlerProc(kAENoDispatch),
- 0 );
- if( tErr == noErr )
- {
- //———— If it was resumed properly we should not dispose of it later
- fSuspended = false;
- }
-
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::PutReturnValueIntoReplyEvent - Convert the ScriptValue
- // to an AE Descriptor.
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- AERequest::PutReturnValueIntoReplyEvent()
- {
- OSErr tErr;
- Boolean tBoolean;
- short tShort;
- long tLong;
- char* tText;
- unsigned short tTextSize;
- AEDescList tDescList;
- long tVUAESrvcResults;
-
- //———— APPLESCRIPT COMPATIBILITY CHECK:
- //———— Put the value into the reply event according to its kind
- //———— note: VU 2.1 recognizes non-standard and standard results
- if( this->UseStandardReplyType() )
- {
- tVUAESrvcResults = kVUAESrvcResults; //———— AppleScript and VU 2.1
- }
- else
- {
- tVUAESrvcResults = kVUAEOldSrvcResults; //———— VU 2.0, 2.0.1, and 2.1
- }
-
- //———— Put the value into the reply event according to its kind
- switch( fReturnValue->GetValueKind() )
- {
- case kVUBooleanKind:
- {
- tBoolean = ((VUBoolean*)fReturnValue)->GetBoolean();
- if( tBoolean )
- {
- tErr = AEPutParamPtr( fReplyPtr,
- tVUAESrvcResults,
- typeTrue,
- (Ptr)&tBoolean,
- sizeof(Boolean) );
- }
- else
- {
- tErr = AEPutParamPtr( fReplyPtr,
- tVUAESrvcResults,
- typeFalse,
- (Ptr)&tBoolean,
- sizeof(Boolean) );
- }
- if( tErr != noErr )
- {
- SetErrorCode( tErr );
- SetErrorMessage( "Failed to install return value into the reply." );
- }
- }
- break;
-
- case kVUNumberKind:
- {
- tShort = ((VUNumber*)fReturnValue)->GetNumber();
- tErr = AEPutParamPtr( fReplyPtr,
- tVUAESrvcResults,
- typeShortInteger,
- (Ptr)&tShort,
- sizeof(short) );
- if( tErr != noErr )
- {
- SetErrorCode( tErr );
- SetErrorMessage( "Failed to install return value into the reply." );
- }
- }
- break;
-
- case kVULongNumberKind:
- {
- tLong = ((VULongNumber*)fReturnValue)->GetNumber();
- tErr = AEPutParamPtr( fReplyPtr,
- tVUAESrvcResults,
- typeLongInteger,
- (Ptr)&tLong,
- sizeof(long) );
- if( tErr != noErr )
- {
- SetErrorCode( tErr );
- SetErrorMessage( "Failed to install return value into the reply." );
- }
- }
- break;
-
- case kVUStringKind:
- {
- tText = ((VUString*)fReturnValue)->GetText();
- if( tText != nil )
- {
- if( (tTextSize = strlen( tText )) > 0)
- {
- tErr = AEPutParamPtr( fReplyPtr,
- tVUAESrvcResults,
- typeChar,
- (Ptr)tText,
- tTextSize );
- if( tErr != noErr )
- {
- SetErrorCode( tErr );
- SetErrorMessage( "Failed to install return value into the reply." );
- }
- }
- }
- }
- break;
-
- case kVUListKind:
- {
- tErr = MakeDescListFromVUList( (VUList*)fReturnValue, &tDescList );
- if( tErr == noErr )
- {
- tErr = AEPutParamDesc( fReplyPtr,
- tVUAESrvcResults,
- &tDescList );
- AEDisposeDesc( &tDescList );
- }
- }
- break;
-
- default:
- {
-
- }
- break;
- }
-
-
- return tErr;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::MakeDescListFromVUList - Convert the VUList
- // to a pDescList object.
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- AERequest::MakeDescListFromVUList( VUList* pValue, AEDescList* pDescList )
- {
- OSErr tErr;
- short tItemCount;
- short i;
- ScriptValue* tVal;
- Boolean tBoolean;
- short tShort;
- long tLong;
- char* tText;
- AEDescList tDescList;
- ValueKind tValueKind;
-
- //———— Create the AEDescList to fill with the values in the VUList
- tErr = AECreateList( nil, 0, false, pDescList );
- if( tErr != noErr )
- {
- return tErr;
- }
- //———— Get the number of items in the VUList
- tItemCount = pValue->GetCount();
-
- //———— Put each item of the VUList, into the AEDescList
- for( i = 1; i <= tItemCount; i++ )
- {
- //———— Get the ith ScriptValue object from the VUList
- tValueKind = kVUAnyKind;
- tErr = pValue->GetNthItem( i, tVal, tValueKind );
- if( tVal )
- {
- //———— if we got one, process it according to its kind
- switch( tValueKind )
- {
-
- //———— VUBoolean object
- case kVUBooleanKind:
- {
- tBoolean = ((VUBoolean*)tVal)->GetBoolean();
- tErr = AEPutPtr( pDescList,
- i,
- typeBoolean,
- (Ptr)&tBoolean,
- sizeof(Boolean) );
- }
- break;
-
- //———— VUNumber object
- case kVUNumberKind:
- {
- tShort = ((VUNumber*)tVal)->GetNumber();
- tErr = AEPutPtr( pDescList,
- i,
- typeShortInteger,
- (Ptr)&tShort,
- sizeof(short) );
- }
- break;
-
- //———— VULongNumber object
- case kVULongNumberKind:
- {
- tLong = ((VULongNumber*)tVal)->GetNumber();
- tErr = AEPutPtr( pDescList,
- i,
- typeLongInteger,
- (Ptr)&tLong,
- sizeof(long) );
- }
- break;
-
- //———— VUString object
- case kVUStringKind:
- {
- tText = ((VUString*)tVal)->GetText();
- if( tText )
- {
- tErr = AEPutPtr( pDescList,
- i,
- typeChar,
- (Ptr)tText,
- strlen(tText) );
- }
- }
- break;
-
- //———— VUList object
- case kVUListKind:
- {
- tErr = MakeDescListFromVUList( (VUList*)tVal, &tDescList );
- if( tErr == noErr )
- {
- tErr = AEPutDesc( pDescList,
- i,
- &tDescList );
- AEDisposeDesc( &tDescList );
- }
- }
- break;
-
- default:
- {
- }
- break;
- }
- //———— Break out of for loop, if there was an error
- if( tErr != noErr )
- {
- break;
- }
- }
- }
-
- //———— if there was an error, dispose of the AEDescList created earlier
- if( tErr != noErr )
- {
- AEDisposeDesc( &tDescList );
- }
- return tErr;
- }
-
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // AERequest::FixAEManagerBugPart1 - copy the Apple Event and its result to fix a bug.
- //———— Duplicate the AppleEvent Record, and reset the pointers
- //———— SBR 12/01/94: This is to fix a bug in the AE Manager, which is
- //———— kind enough to trash the AppleEvent sometime after you suspend it.
- //———— All code uses fMessagePtr and fReplyPtr, so this is transparent.
- //—————————————————————————————————————————————————————————————————————————————————————
- void
- AERequest::FixAEManagerBugPart1()
- {
- OSErr tErr = noErr;
-
- gTheApplication->fMessagePtrOrig = nil;
- gTheApplication->fReplyPtrOrig = nil;
-
- //———— use true to fix this bug the old way, false for the new way
- if( true )
- {
- //———— The method used in the old template may not always work,
- //———— because it does not duplicate embedded AEDesc's that
- //———— have separate memory allocation. When these are disposed
- //———— by AEResumeTheCurrentEvent(), what happens to the
- //———— embedded AEDesc's in the original (trashed) AppleEvent?
-
- //———— Note: in practice, this method has worked just fine!
- fMessage = *fMessagePtr;
- fMessagePtr = &fMessage;
-
- fReply = *fReplyPtr;
- fReplyPtr = &fReply;
-
- return;
- }
- else
- {
- //———— Save the original pointers for debugging this problem.
- //———— Once the copies are made, we can delete the originals,
- //———— but we can not delete them now, while in their handler.
- //———— They are deleted in Request::FixAEManagerBugPart2(),
- gTheApplication->fMessagePtrOrig = fMessagePtr;
- gTheApplication->fReplyPtrOrig = fReplyPtr;
-
- //———— Duplicate the message AppleEvent Record
- tErr = AEDuplicateDesc( fMessagePtr, &fMessage );
-
- if( tErr != noErr )
- {
- DebugStr( "\pAERequest::FixAEManagerBugPart1 Error AEDuplicateDesc fMessagePtr, &fMessage." );
- }
- fMessagePtr = &fMessage;
-
- //———— Duplicate the reply AppleEvent Record
- tErr = AEDuplicateDesc( fReplyPtr, &fReply );
-
- if( tErr != noErr )
- {
- DebugStr( "\pAERequest::FixAEManagerBugPart1 Error AEDuplicateDesc fReplyPtr, &fReply.");
- }
- fReplyPtr = &fReply;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- /*
- if( tDo201Reply ) //———— Send a VU 2.0.1 version of this AppleEvent reply
- {
- Size tSize;
- DescType tType;
- AEDesc tDesc;
- OSType tVUEventClass = kVUAETool;
- AppleEvent tReplyFrom201Send;
-
- //———— Duplicate the reply AppleEvent Record
- tErr = AEDuplicateDesc( fReplyPtr, &fReply201 );
- fReply201Ptr = &fReply201;
-
- if( tErr != noErr )
- {
- DebugStr("\pAEPutAttributePtr Error AEDuplicateDesc fReplyPtr, &fReply201.");
- }
- //———— Change the 201 reply's class id to 'v.u.'
- //———— Do this before any errors can occur
- tErr = AEPutAttributePtr( fReply201Ptr,
- keyEventClassAttr,
- typeType,
- (Ptr) &tVUEventClass,
- sizeof( OSType ) );
- if( tErr != noErr )
- {
- DebugStr("\pAEPutAttributePtr Error Change the 201 reply class.");
- }
-
-
- if( fReturnValue != nil )
- {
- //———— Move the reply data from the direct parameter
- //———— to the the kVUAESrvcResults201 parameter
- tErr = AEGetParamDesc( fReplyPtr,
- kVUAESrvcResults,
- typeWildCard,
- &tDesc );
- if( tErr != noErr )
- {
- DebugStr("\pAEGetParamDesc Error Move the reply data.");
- }
-
-
- if( tErr == noErr )
- tErr = AEPutParamDesc( fReply201Ptr,
- kVUAESrvcResults201,
- &tDesc );
- if( tErr != noErr )
- {
- DebugStr("\pAEPutParamDesc Error AEPutParamDesc fReply201Ptr kVUAESrvcResults201.");
- }
-
-
-
- //———— Delete the data from the direct parameter
- if( tErr == noErr )
- tErr = AEDeleteParam( fReply201Ptr,
- kVUAESrvcResults );
- if( tErr != noErr )
- {
- DebugStr("\pAEDeleteParam Error AEDeleteParam fReply201Ptr.");
- }
-
- }
-
- //———— Send the VU 2.0.1 reply AppleEvent
- if( tErr == noErr )
- tErr = AESend( fReply201Ptr,
- &tReplyFrom201Send,
- kAENoReply,
- kAENormalPriority,
- kNoTimeOut,
- nil, nil );
- if( tErr != noErr )
- {
- DebugStr("\pAESend Error AESend fReply201Ptr.");
- }
-
-
- if( tErr == noErr )
- tErr = AEDisposeDesc( &tDesc );
- if( tErr != noErr )
- {
- DebugStr("\pAEDisposeDesc Error creating VU 2.0.1 reply AppleEvent.");
- }
-
-
- if( fReply201Ptr != nil )
- {
- AEDisposeDesc( fReply201Ptr );
- fReply201Ptr = nil;
- }
-
- if( tErr != noErr )
- {
- DebugStr("\pError creating VU 2.0.1 reply AppleEvent.");
- }
-
- }
- */
-
-